package com.haha;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

public class PaladinNqActionListener implements ActionListener {

    private final List<Integer> centerList;
    private final WinDef.RECT wowWindow = getWorldOfWarcraftWindow();
    private final Color red = new Color(255, 0, 0);
    private final Color green = new Color(0, 255, 0);
    int topAdd = 3;
    private Robot robot;

    public PaladinNqActionListener(List<Integer> centerList) {
        this.centerList = centerList;
        try {
            robot = new Robot();
        } catch (AWTException awtException) {
            awtException.printStackTrace();
            System.out.println("初始化失败");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 获取道标的开关
        int winLeft = wowWindow.left + centerList.get(1);
        int winTop = wowWindow.top + topAdd;
        Color dbSwitchColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(dbSwitchColor)) {
            // 自动道标检测开关打开
            // 下面找到第1个色块，看是否需要上道标
            winLeft = wowWindow.left + centerList.get(0);
            winTop = wowWindow.top + topAdd;
            Color dbColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(dbColor)) {
                robot.keyPress(KeyEvent.VK_F);
                robot.keyRelease(KeyEvent.VK_F);
                return;
            }
        }

        // 获取圣光术的开关
        winLeft = wowWindow.left + centerList.get(3);
        winTop = wowWindow.top + topAdd;
        Color sgsSwitchColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(sgsSwitchColor)) {
            winLeft = wowWindow.left + centerList.get(2);
            winTop = wowWindow.top + topAdd;
            Color sgsColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(sgsColor)) {
                robot.keyPress(KeyEvent.VK_2);
                robot.keyRelease(KeyEvent.VK_2);
                return;
            }
            winLeft = wowWindow.left + centerList.get(4);
            winTop = wowWindow.top + topAdd;
            Color sgsxColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(sgsxColor)) {
                robot.keyPress(KeyEvent.VK_1);
                robot.keyRelease(KeyEvent.VK_1);
                return;
            }
        }
    }

    private WinDef.RECT getWorldOfWarcraftWindow() {
        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "魔兽世界");
        WinDef.RECT winRect = new WinDef.RECT();
        User32.INSTANCE.GetWindowRect(hwnd, winRect);
        return winRect;
    }
}
